package com.wrapper;

import com.alibaba.fastjson.annotation.JSONField;
import com.enums.RespCodeEnums;

import java.io.Serializable;

public class ResponseWrapper<T> implements Serializable {

    /**默认0成功**/
    private int code = 0;
    /**默认999002成功**/
    private int respCode = 999002;
    /**对外提示消息**/
    private String message;
    /**内部消息**/
    private String innerMessage;

    public String getInnerMessage() {
        return innerMessage;
    }

    public void setInnerMessage(String innerMessage) {
        this.innerMessage = innerMessage;
    }

    @JSONField(name = "result", alternateNames = {"result","document"})
    private T result;

    public ResponseWrapper() {
        super();
    }

    public ResponseWrapper(T result) {
        super();
        this.result = result;
    }

    public ResponseWrapper(int code, String message) {
        super();
        this.code = code;
        this.message = message;
        this.innerMessage=message;
    }

    public ResponseWrapper(int code, String message, int respCode) {
        super();
        this.code = code;
        this.message = message;
        this.respCode = respCode;
        this.innerMessage=message;
    }

    public ResponseWrapper(int code, String message, int respCode, String innerMessage) {
        super();
        this.code = code;
        this.message = message;
        this.respCode = respCode;
        this.innerMessage= innerMessage;
    }

    public ResponseWrapper(int code, String message, T result) {
        super();
        this.code = code;
        this.message = message;
        this.result = result;
        if (RespCodeEnums.SUCCESS_CODE.code == this.code) {
            this.respCode = RespCodeEnums.SUCCESS_RESPCODE.code;
        } else {
            this.respCode = RespCodeEnums.FAILED_RESPCODE.code;
        }
    }

    public static <T> ResponseWrapper<T> success(T result) {
        ResponseWrapper<T> wrapper = new ResponseWrapper<T>(RespCodeEnums.SUCCESS_CODE.code,"成功");
        wrapper.result = result;
        return wrapper;
    }

    public static <T> ResponseWrapper success(int respCode, T result) {
        ResponseWrapper<T> wrapper = new ResponseWrapper(RespCodeEnums.SUCCESS_CODE.code,"成功");
        wrapper.respCode = respCode;
        wrapper.result = result;
        return wrapper;
    }

    public static <T> ResponseWrapper fail(T result) {
        ResponseWrapper<T> wrapper = new ResponseWrapper(RespCodeEnums.FAILED_CODE.code,"失败");
        wrapper.result = result;
        return wrapper;
    }

    public static <T> ResponseWrapper fail(int respCode, T result) {
        ResponseWrapper<T> wrapper = new ResponseWrapper(RespCodeEnums.FAILED_CODE.code,"失败");
        wrapper.respCode = respCode;
        wrapper.result = result;
        return wrapper;
    }

    public static <T> ResponseWrapper fail(int respCode, T result, String innerMessage) {
        ResponseWrapper<T> wrapper = new ResponseWrapper(RespCodeEnums.FAILED_CODE.code,"失败");
        wrapper.respCode = respCode;
        wrapper.result = result;
        wrapper.innerMessage = innerMessage;
        return wrapper;
    }

    public static <T> ResponseWrapper fail(int respCode, String msg, String innerMessage) {
        ResponseWrapper<T> wrapper = new ResponseWrapper(RespCodeEnums.FAILED_CODE.code, msg);
        wrapper.respCode = respCode;
        wrapper.innerMessage = innerMessage;
        return wrapper;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getRespCode() {
        return respCode;
    }

    public void setRespCode(int respCode) {
        this.respCode = respCode;
    }
}
